package com.book.iphone;

import com.book.book.domain.BooksDO;
import com.book.book.service.BooksService;
import com.book.book.service.SolrService;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.MapSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by siyuan on 2018/3/15.
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@Controller
@RequestMapping("/iphone")
public class SolrController {

    @Autowired
    private SolrService solrService;
    @Autowired
    private BooksService booksService;
    @Autowired
    private SolrClient client;

    @RequestMapping("/add")
    public String addBook() throws IOException, SolrServerException {

        // 定义数据 solr输入文档 数据结构为Map
        SolrInputDocument inputDocument = new SolrInputDocument();
        inputDocument.addField("id", "1231111");
        inputDocument.addField("book_name", "图书名称");
        inputDocument.addField("author2", "作者");
        // 执行添加 ps：如果id相同，则执行更新操作
        // 要指定操作的collection 就是solr-home下定义的core
        UpdateResponse add = client.add("collection1", inputDocument);
        //提交添加/更新
        client.commit("collection1");
        System.out.println("sss");
        return "/";
    }

    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public String queryBook(@RequestParam("bookName") String bookName, BooksDO booksDO, Model model) throws IOException, SolrServerException {

        // 定义查询条件
        Map<String, String> params = new HashMap<String, String>();
        // 输入查询 的参数
//       String bookName=booksDO.getBookName();
        if (bookName == null || bookName.trim().equals("")) {
            bookName = "*";
        }
        params.put("q", "book_name:" + bookName);
        SolrParams mapSolrParams = new MapSolrParams(params);
        //执行查询 第一个参数是collection，就是我们在solr中创建的core
        QueryResponse response = client.query("collection1", mapSolrParams);
        // 获取结果集
        SolrDocumentList results = response.getResults();
        model.addAttribute("results", results);

        if (results.size() == 0) {
            model.addAttribute("results", null);
        } else {
            model.addAttribute("results", results);
            for (SolrDocument result : results) {
                // SolrDocument 数据结构为Map
                result.setField("book_img",
                        (String) result.getFieldValue("book_img").toString().split(",")[0]);
            }
        }
        return "book/phone/html/searchlist";
    }

    @RequestMapping("/delete")
    public String deleteBook() throws IOException, SolrServerException {

        // 通过id删除 执行要删除的collection（core）
        client.deleteById("collection1", "123");
        // 还可以通过查询条件删除
        // solrClient.deleteByQuery("RoleVO", "查询条件");
        // 提交删除
        client.commit("collection1");
        return "/";
    }

//    @RequestMapping(value = "/search", method = RequestMethod.POST)
//    String Re(){
//        return "/book/phone/html/searchlist";
//    }



}


